package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpProgramTemplateService;
import com.ck.constant.ResponseConstant;
import com.ck.model.common.ProgramTemplate;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.common.sys.LoginUser;
import com.ck.util.SessionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 节目模板
 *
 * @author linqiang
 * @version 2.0
 * @date 2018-09-11 11:50
 */
@PermissionRequired
@Controller
@RequestMapping("/client/program/template")
public class CpProgramTemplateClientController {
    private static Logger logger = LoggerFactory.getLogger(CpProgramTemplateClientController.class);

    @Autowired
    private CpProgramTemplateService cpProgramTemplateService;

    /**
     * 新增模板
     *
     * @param json         模板内容
     * @param screenWidth  宽
     * @param screenHeight 高
     * @param backupImgUrl 背景
     * @param request      请求
     * @return Object
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(String name, String json, Integer screenWidth, Integer screenHeight, String backupImgUrl, HttpServletRequest request) {
        if (StringUtils.isBlank(name)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "模板名称不能为空");
        }
        if (null == screenWidth || null == screenHeight) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "模板尺寸不能为空");
        }
        try {
            LoginUser loginUser = SessionUtils.getLoginUserInfo(request);
            ProgramTemplate check = cpProgramTemplateService.selectByName(name);
            if (check != null) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "模板名称已存在，请重新填写");
            }
            Long id = cpProgramTemplateService.save(name, json, screenWidth, screenHeight, backupImgUrl, loginUser);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, id, "新增节目模板成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramTemplateController saveProgramTemplate 新增节目模板失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "新增节目模板失败");
    }

    /**
     * 修改节目模板
     *
     * @param templateId   模板ID
     * @param json         模板内容
     * @param backupImgUrl 模板背景图片
     * @param name         模板名称
     * @param request      请求
     * @return JsonResponse
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(Long templateId, String json, String backupImgUrl, String name, HttpServletRequest request) {
        if (null == templateId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "模板ID不能为空");
        }
        try {
            if(StringUtils.isNotBlank(name)){
                ProgramTemplate old = cpProgramTemplateService.selectByPrimaryKey(templateId);
                if (old != null && !old.getName().equals(name)) {
                    //修改了名称，检查唯一
                    ProgramTemplate check = cpProgramTemplateService.selectByName(name);
                    if (check != null) {
                        return new JsonResponse(ResponseConstant.CODE_FAIL, "模板名称已存在，请重新填写");
                    }
                }
            }
            LoginUser loginUser = SessionUtils.getLoginUserInfo(request);
            cpProgramTemplateService.update(templateId, json, backupImgUrl, name, loginUser);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, "修改节目模板成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramTemplateController updateProgramTemplate 修改节目模板失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "修改节目模板失败");
    }
}
